//
//  PhotoManager.m
//  BabyHelper
//
//  Created by  thomas on 11-8-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PhotoManager.h"


@implementation PhotoManager

static PhotoManager *_instance = nil;
static NSMutableDictionary *babyGrowPhotoThumDic = nil;
static NSMutableDictionary *babyPhotoThumDic = nil;
static NSMutableArray *babyGrowPhotos;
static NSMutableArray *babyPhotos;

+(PhotoManager *) getInstance {
	
	@synchronized(self) {
		
		if(_instance == nil) {
			
			_instance = [[PhotoManager alloc] init];
		
			babyGrowPhotoThumDic = [[NSMutableDictionary alloc] init];
			babyPhotoThumDic     = [[NSMutableDictionary alloc] init];
			
			Dao *dao = [Dao getInstance];
			
			babyGrowPhotos = [[NSMutableArray alloc]init];
			
			[dao QueryBabyPhotoEntryWith:@"where BABYGROWID > 0 and PHOTOPATH != 'null'" records:babyGrowPhotos];
			
			for(int i=0; i<[babyGrowPhotos count];i++)
			{
				BabyPhotoEntry *entry = [babyGrowPhotos objectAtIndex:i];
				UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),entry.thumPath]];
				if(image != nil)
				[babyGrowPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:entry.index]];
			}
			//////////////
			babyPhotos = [[NSMutableArray alloc]init];
			
			[dao QueryBabyPhotoEntryWith:@"where BABYGROWID < 0 and PHOTOPATH != 'null'" records:babyPhotos];
			
			for(int i=0; i<[babyPhotos count];i++)
			{
				BabyPhotoEntry *entry = [babyPhotos objectAtIndex:i];
				UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),entry.thumPath]];
				if(image != nil)
					[babyPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:entry.index]];
			}
			
		}
	}
	
	return _instance;
}

-(UIImage *)GetThumPhotoByBabyGrowID:(int)photoID
{
	return [babyGrowPhotoThumDic objectForKey:[NSNumber numberWithInt:photoID]];
}

-(void)UpdateThumImageByBabyGrowID:(int)photoID image:(UIImage *)image;
{
	[image retain];
	[babyGrowPhotoThumDic removeObjectForKey:[NSNumber numberWithInt:photoID]];
	[babyGrowPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:photoID]];
}

-(void)InsertThumImageByBabyGrowID:(int)photoID image:(UIImage *)image;
{
	if (image != nil) {
		[babyGrowPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:photoID]];		
	}
}

-(void)RemoveThumImageByBabyGrowID:(int)photoID;
{
	[babyGrowPhotoThumDic removeObjectForKey:[NSNumber numberWithInt:photoID]];
}
//////////////////////

-(UIImage *)GetThumPhotoByPhotoID:(int)photoID
{
	return [babyPhotoThumDic objectForKey:[NSNumber numberWithInt:photoID]];
}

-(void)UpdateThumImageByPhotoID:(int)photoID image:(UIImage *)image;
{
	[image retain];
	[babyPhotoThumDic removeObjectForKey:[NSNumber numberWithInt:photoID]];
	[babyPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:photoID]];
}

-(void)InsertThumImageByPhotoID:(int)photoID image:(UIImage *)image;
{
	if (image != nil) {
		[babyPhotoThumDic setObject:image forKey:[NSNumber numberWithInt:photoID]];		
	}
}

-(void)RemoveThumImageByPhotoID:(int)photoID;
{
	[babyPhotoThumDic removeObjectForKey:[NSNumber numberWithInt:photoID]];
}
@end
